<?php

class Reportes extends Controller {

    public function  __construct()
    {
        parent::Controller();
        $this->load->database();
        $this->load->library('session');
    }

    
    public function index()
    {
        $this->load->view('cliente/index');
    }
    
    
    public function verReporte()
    {
        $this->load->view('reportes/verReporte');
    }
    
    
	public function ingresarCliente()
    {
        $this->load->view('cliente/ingresarCliente');
    }
    
    
    public function editarCliente()
    {
        $this->load->view('cliente/editarCliente');
    }
    
    
    public function eliminarCliente()
    {
        $this->load->view('cliente/eliminarCliente');
    }
    
    
	public function clienteSelect()
    {
        $start = isset($_REQUEST['start']) ? $_REQUEST['start'] : 0;
		$limit = isset($_REQUEST['limit']) ? $_REQUEST['limit'] : 20;

        if (isset($_POST['query']))
        {
            $this->db->like('cliente.nombre_fantasia', $_POST['query']);
            $this->db->or_like('cliente.razon_social', $_POST['query']);
            $this->db->or_like('cliente.email', $_POST['query']);
            $this->db->or_like('cliente.rut', $_POST['query']);
            $this->db->or_like('cliente.giro', $_POST['query']);
            $this->db->or_like('cliente.direccion', $_POST['query']);
            $this->db->or_like('cliente.plazo_pago', $_POST['query']);
        }

        $this->db->select('cliente.*');
        $this->db->from('cliente');
        $this->db->order_by('cliente.nombre_fantasia');

        $this->db->limit($limit, $start);

        $query = $this->db->get();
        $results = $this->db->count_all('cliente');
        $arr = array();
        foreach ($query->result() as $obj)
        {
            $arr[] = $obj;
        }
        echo '{success:true,results:'. $results .
                ',rows:'.json_encode($arr).'}';
    }

    
    public function clienteUpdate()
    {
            $data = array(
            	'idCliente' 			=> $field[0],
                'razon_social' 	=> $field[1],
                'nombre_fantasia'=> $field[2],
                'direccion' 	=> $field[3],
                'telefono' 		=> $field[4],
            	'email' 		=> $field[5],
            	'condpago' 		=> $field[6],
            		
            );
                $this->db->where('idCliente', $field[0]);
                $this->db->update('cliente', $data);
    }

    
    public function clienteDelete()
    {
        $records = explode(';', $_POST['postdata']);
        foreach($records as $id)
        {
            $this->db->where('idCliente', $id);
            $this->db->delete('cliente');
        }
    }
    
    
    public function clienteInsert()
    {
    	if($_POST){
    		  
            $data = array(
            	'nombrefantasia'		 => $_POST['nombrefantasia'],
                'razonsocial' 	 		 => $_POST['razonsocial'],
                'rut'					 => $_POST['rut'],
                'giro' 	        		 => $_POST['giro'],
                'direccion'     		 => $_POST['direccion'],
                'telefono' 		 		 => $_POST['telefono'],
            	'email' 		 		 => $_POST['email'],
            	'plazopago'      		 => $_POST['plazopago'],
                'montovenc'     		 => $_POST['montovenc'],
                'suspensiondespacho'     => $_POST['suspensiondespacho'],
                'nombredespacho'         => $_POST['nombredespacho'],            
                'cargodespacho'          => $_POST['cargodespacho'],
                'fonodespacho'   		 => $_POST['fonodespacho'],
                'emaildespacho'  		 => $_POST['emaildespacho'],            
                'horarec'        		 => $_POST['horarecdespacho'],
                'nombrecontacto' 		 => $_POST['nombrecontacto'],            
                'cargocontacto'  		 => $_POST['cargocontacto'],
                'fonocontacto'   		 => $_POST['fonocontacto'],
                'emailcontacto' 		 => $_POST['emailcontacto'], 
                'horacob'        		 => $_POST['horacob']
            );
            
        	$this->load->model('clientemodel');
            $this->clientemodel->insertarFormCliente($data);
            echo "<script  language='javascript'>alert('Nuevo Cliente ingresado exitosamente!');</script>";
            $this->ingresarCliente();
            
    	}else{
    		echo "<script  language='javascript'>alert('Error: No existen datos para ingresar!');</script>";
    	    $this->ingresarCliente();
    	}    	
    }
    
    
    
}
